# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=theano
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.0.5
pkgrel=1
pkgdesc="Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently (mingw-w64)"
arch=('any')
url="https://github.com/Theano/Theano"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-scipy"
         "${MINGW_PACKAGE_PREFIX}-python-six")
optdepends=("${MINGW_PACKAGE_PREFIX}-gcc: Support to execute optimized C-implementations (for both CPU and GPU)")
makedepends=("patch" "${MINGW_PACKAGE_PREFIX}-python-setuptools")
_archive=Theano-rel-${pkgver}
install=${_realname}3-${CARCH}.install
source=(${_archive}.tar.gz::https://github.com/Theano/Theano/archive/rel-${pkgver}.tar.gz
        0001-cmodule-obtain-mingw-w64-python-library.patch
        0002-not-use-np.distutils.__config__.blas_opt_info.patch)
sha256sums=('48a0a4bab7fdbeb5aa92e8668b05fb09c3d86670fdcad9c3774e970acbd33bdf'
            'fd4a66aac925abdd9316bb142784c97e63d42ef590dddddf5d1771e69ea189df'
            '532ed23eadd9623fb87c7c50e25f64d2b68fd24cb4b9f546be2a154685b5799a')
noextract=(${_archive}.tar.gz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -b -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"
  bsdtar zxf ${_archive}.tar.gz || true

  cd "${srcdir}/${_archive}"
  apply_patch_with_msg \
    0001-cmodule-obtain-mingw-w64-python-library.patch \
    0002-not-use-np.distutils.__config__.blas_opt_info.patch

  cd "${srcdir}"
  rm -rf "python-build-${CARCH}" | true
  cp -r "${_archive}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py --quiet build
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py --quiet install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  install -Dm644 doc/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
